﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Sharepoint.Client.Wrapper
{
    //Структура для выдачи результатов
    public class ListStruct
    {
        private const String DispForm = "DispForm.aspx";
        public String Name { get; set; }
        public String FileRef { get; set; }
        public String FileDirRef { get; set; }
        public String FileLeafRef { get; set; }
        public String ListDirRef { get; set; }
        public Guid Guid { get; set; }
        public Guid ListId { get; set; }
        public int ItemId { get; set; }

        public Uri Uri { get; set; }

        public String HostName 
        {
            get
            { 
                return String.Format("{0}://{1}{2}", this.Uri.Scheme, this.Uri.Host, (this.Uri.Port == 80 || this.Uri.Port == 443) ? "" : ":");
            }
     
        }

        public String DownloadLink 
        {
            get 
            {
                if (this.IsLibrary)
                {
                    return String.Format("{0}{1}", this.HostName, this.FileRef);
                }
// not library
                return String.Format("{0}{1}/{2}?ID={3}", this.HostName, this.FileDirRef, DispForm, this.ItemId);
            }
        
        }

        public ListStructs childs { get; set; }
        public Boolean hasChild
        {
            get
            {
                return this.childs.Count == 0 ? false : true;
            }
        }
        public DateTime Created { get; set; }

        public Boolean IsLibrary { get; set; }

        public Boolean IsFolder { get; set; }

        public Boolean IsList { get; set; }

        public Boolean IsFile
        {
            get
            {
                return this.FileLeafRef == String.Empty ? false : true;
            }
        }

        public ListStruct()
        {
            this.Name = "Empty";
        }

        public ListStruct(String _Name)
        {
            this.Name = _Name;
        }

        public ListStruct(String _Name, Uri _Uri)
        {
            this.Name = _Name;
            this.Uri = _Uri;
        }

    }


    public class ListStructs : IEnumerable
    {

        private System.Collections.Generic.List<ListStruct> _listType;

        // Количество элементов
        public int Count
        {
            get
            {
                return _listType.Count;
            }
        }
        // добавить новый элемент

        public ListStruct Add(ListStruct _listStruct)
        {
            _listType.Add(_listStruct);
            return _listStruct;
        }

        public ListStructs()
        {
            _listType = new List<ListStruct>();
        }
        ~ListStructs()
        {
            _listType = null;
        }

        public IEnumerator GetEnumerator()
        {
            return _listType.GetEnumerator();
        }

        public ListStruct nameFind(String _name2find)
        {
            return _listType.Find(x => x.Name == _name2find);
        }

        public ListStruct indexFind(int _index2find)
        {
            if (_listType[_index2find] == null)
                throw new ArgumentOutOfRangeException("Index out of Range", "Index out of Range"); // remake!

            return _listType[_index2find];
        }

    }
}
